import java.util.Arrays;
import java.util.PriorityQueue;

public class Test {
}
class Solution {
    public int[] minInterval(int[][] intervals, int[] queries) {
        int[] ans = new int[queries.length];
        Arrays.fill(ans,-1);
        Arrays.sort(intervals,(o1,o2) -> (o1[0]-o2[0]));
        int[][] tmp = new int[queries.length][2];
        for(int i=0; i<queries.length; i++){
            tmp[i][0] = queries[i];
            tmp[i][1] = i;
        }
        Arrays.sort(tmp,(o1,o2)->(o1[0]-o2[0]));
        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2)->(o1[1]-o1[0]-o2[1]+o2[0]));
        int index = 0;
        for(int i=0; i < queries.length; i++){
            while(index<intervals.length && tmp[i][0] >= intervals[index][0]){
                queue.offer(new int[]{intervals[index][0],intervals[index][1]});
                index++;
            }
            while(!queue.isEmpty() && tmp[i][0] > queue.peek()[1]){
                queue.poll();
            }
            if(!queue.isEmpty()){
                ans[tmp[i][1]] = queue.peek()[1]-queue.peek()[0]+1;
            }
        }
        return ans;
    }
}
